// pages/perCenter/disOrder/disOrder.js
var util = require('../../../utils/util.js');
var dateUtil = require('../../../utils/data.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
var GetList = function (that) {
  let token = wx.getStorageSync('token');
  wx.showNavigationBarLoading();

  util.request(api.SpreaderList, { token: token, type: 3 }).then(function (res) {
    wx.hideNavigationBarLoading();
    if (res.code === '200') {
      that.setData({
        users: res.datas,
        customerNum: res.datas.length
      })
    }
  });
  util.request(api.SpreaderList, { token: token, type: 2 }).then(function (res) {
    wx.hideNavigationBarLoading();
    if (res.code === '200') {
      that.setData({
        spreaders: res.datas,
        spreaderNum: res.datas.length
      })
    }
  });
  util.request(api.SpreadHasNew, { token: wx.getStorageSync("token") }).then(function (res) {
    if (res.code == '200') {
      that.setData({
        hasNewSpread: res.datas
      })
    }
  });
}
Page({
  data: {
    // tab切换 
    currentTab: 0,
    // 页面配置  
    winWidth: 0,
    winHeight: 0,
    hasNewSpread: "",
    users: [],
    spreaders: [],
    spreaderNum:0,
    customerNum: 0
  },
  onLoad: function () {
    var that = this;
    let token = wx.getStorageSync('token');
    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    GetList(this);

  },

  // 滑动切换tab 
  bindChange: function (e) {
    var current = e.detail.current;
    var that = this;

    that.setData({
      currentTab: current,
    })
  
    GetList(this);

  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;

    that.setData({
      currentTab: e.target.dataset.current,
    })

    GetList(this);

  },
  bindReceive(event) {
    var that = this
    wx.showModal({
      title: '警告',
      content: '同意推广人？',
      success: function (res) {
        if (res.confirm) {
          var id = event.currentTarget.dataset.id;
          var auth = "1";
          let token = wx.getStorageSync('token');
          util.request(api.AuditSpreader, { token: token, id: id, auth: "1" }).then(function (res) {
            wx.hideNavigationBarLoading();
            if (res.code === '200') {
              wx.showToast({
                title: '审核成功',
              })
              GetList(that)
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  }
  , bindReject(event) {
    var that = this
    wx.showModal({
      title: '警告',
      content: '拒绝推广人？',
      success: function (res) {
        if (res.confirm) {
          var id = event.currentTarget.dataset.id;
          let token = wx.getStorageSync('token');
          util.request(api.AuditSpreader, { token: token, id: id, auth: "2" }).then(function (res) {
            wx.hideNavigationBarLoading();
            if (res.code === '200') {
              wx.showToast({
                title: '审核成功',
              })
              GetList(that)
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }, invite: function () {

    wx.navigateTo({
      url: "/pages/ucenter/invite/index?shareType=1"
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    GetList(this);

    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  }
})